<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="父id" prop="parentId">
        <el-input
          v-model="queryParams.parentId"
          placeholder="请输入父id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="菜单标题" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入菜单标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="路径" prop="url">
        <el-input
          v-model="queryParams.url"
          placeholder="请输入路径"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="组件" prop="component">
        <el-input
          v-model="queryParams.component"
          placeholder="请输入组件"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="组件名字" prop="componentName">
        <el-input
          v-model="queryParams.componentName"
          placeholder="请输入组件名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="一级菜单跳转地址" prop="redirect">
        <el-input
          v-model="queryParams.redirect"
          placeholder="请输入一级菜单跳转地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="菜单权限编码" prop="perms">
        <el-input
          v-model="queryParams.perms"
          placeholder="请输入菜单权限编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="菜单排序" prop="sortNo">
        <el-input
          v-model="queryParams.sortNo"
          placeholder="请输入菜单排序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="聚合子路由: 1是0否" prop="alwaysShow">
        <el-input
          v-model="queryParams.alwaysShow"
          placeholder="请输入聚合子路由: 1是0否"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="菜单图标" prop="icon">
        <el-input
          v-model="queryParams.icon"
          placeholder="请输入菜单图标"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否路由菜单: 0:不是  1:是" prop="isRoute">
        <el-input
          v-model="queryParams.isRoute"
          placeholder="请输入是否路由菜单: 0:不是  1:是"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否叶子节点:    1:是   0:不是" prop="isLeaf">
        <el-input
          v-model="queryParams.isLeaf"
          placeholder="请输入是否叶子节点:    1:是   0:不是"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否缓存该页面:    1:是   0:不是" prop="keepAlive">
        <el-input
          v-model="queryParams.keepAlive"
          placeholder="请输入是否缓存该页面:    1:是   0:不是"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否隐藏路由: 0否,1是" prop="hidden">
        <el-input
          v-model="queryParams.hidden"
          placeholder="请输入是否隐藏路由: 0否,1是"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否隐藏tab: 0否,1是" prop="hideTab">
        <el-input
          v-model="queryParams.hideTab"
          placeholder="请输入是否隐藏tab: 0否,1是"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input
          v-model="queryParams.description"
          placeholder="请输入描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否添加数据权限1是0否" prop="ruleFlag">
        <el-input
          v-model="queryParams.ruleFlag"
          placeholder="请输入是否添加数据权限1是0否"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="外链菜单打开方式 0/内部打开 1/外部打开" prop="internalOrExternal">
        <el-input
          v-model="queryParams.internalOrExternal"
          placeholder="请输入外链菜单打开方式 0/内部打开 1/外部打开"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="systemId">
        <el-input
          v-model="queryParams.systemId"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="路径Id" prop="pathId">
        <el-input
          v-model="queryParams.pathId"
          placeholder="请输入路径Id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="全路径" prop="fullPath">
        <el-input
          v-model="queryParams.fullPath"
          placeholder="请输入全路径"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['linkage:permission:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['linkage:permission:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['linkage:permission:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['linkage:permission:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="permissionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" />
      <el-table-column label="父id" align="center" prop="parentId" />
      <el-table-column label="菜单标题" align="center" prop="name" />
      <el-table-column label="路径" align="center" prop="url" />
      <el-table-column label="组件" align="center" prop="component" />
      <el-table-column label="组件名字" align="center" prop="componentName" />
      <el-table-column label="一级菜单跳转地址" align="center" prop="redirect" />
      <el-table-column label="菜单类型(0:一级菜单; 1:子菜单:2:按钮权限)" align="center" prop="menuType" />
      <el-table-column label="菜单权限编码" align="center" prop="perms" />
      <el-table-column label="权限策略1显示2禁用" align="center" prop="permsType" />
      <el-table-column label="菜单排序" align="center" prop="sortNo" />
      <el-table-column label="聚合子路由: 1是0否" align="center" prop="alwaysShow" />
      <el-table-column label="菜单图标" align="center" prop="icon" />
      <el-table-column label="是否路由菜单: 0:不是  1:是" align="center" prop="isRoute" />
      <el-table-column label="是否叶子节点:    1:是   0:不是" align="center" prop="isLeaf" />
      <el-table-column label="是否缓存该页面:    1:是   0:不是" align="center" prop="keepAlive" />
      <el-table-column label="是否隐藏路由: 0否,1是" align="center" prop="hidden" />
      <el-table-column label="是否隐藏tab: 0否,1是" align="center" prop="hideTab" />
      <el-table-column label="描述" align="center" prop="description" />
      <el-table-column label="是否添加数据权限1是0否" align="center" prop="ruleFlag" />
      <el-table-column label="按钮权限状态(0无效1有效)" align="center" prop="status" />
      <el-table-column label="外链菜单打开方式 0/内部打开 1/外部打开" align="center" prop="internalOrExternal" />
      <el-table-column label="${comment}" align="center" prop="systemId" />
      <el-table-column label="路径Id" align="center" prop="pathId" />
      <el-table-column label="全路径" align="center" prop="fullPath" />
      <el-table-column label="背景图片路径" align="center" prop="backImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.backImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="菜单背景图片" align="center" prop="backImage1" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['linkage:permission:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['linkage:permission:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改菜单权限对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="父id" prop="parentId">
          <el-input v-model="form.parentId" placeholder="请输入父id" />
        </el-form-item>
        <el-form-item label="菜单标题" prop="name">
          <el-input v-model="form.name" placeholder="请输入菜单标题" />
        </el-form-item>
        <el-form-item label="路径" prop="url">
          <el-input v-model="form.url" placeholder="请输入路径" />
        </el-form-item>
        <el-form-item label="组件" prop="component">
          <el-input v-model="form.component" placeholder="请输入组件" />
        </el-form-item>
        <el-form-item label="组件名字" prop="componentName">
          <el-input v-model="form.componentName" placeholder="请输入组件名字" />
        </el-form-item>
        <el-form-item label="一级菜单跳转地址" prop="redirect">
          <el-input v-model="form.redirect" placeholder="请输入一级菜单跳转地址" />
        </el-form-item>
        <el-form-item label="菜单权限编码" prop="perms">
          <el-input v-model="form.perms" placeholder="请输入菜单权限编码" />
        </el-form-item>
        <el-form-item label="菜单排序" prop="sortNo">
          <el-input v-model="form.sortNo" placeholder="请输入菜单排序" />
        </el-form-item>
        <el-form-item label="聚合子路由: 1是0否" prop="alwaysShow">
          <el-input v-model="form.alwaysShow" placeholder="请输入聚合子路由: 1是0否" />
        </el-form-item>
        <el-form-item label="菜单图标" prop="icon">
          <el-input v-model="form.icon" placeholder="请输入菜单图标" />
        </el-form-item>
        <el-form-item label="是否路由菜单: 0:不是  1:是" prop="isRoute">
          <el-input v-model="form.isRoute" placeholder="请输入是否路由菜单: 0:不是  1:是" />
        </el-form-item>
        <el-form-item label="是否叶子节点:    1:是   0:不是" prop="isLeaf">
          <el-input v-model="form.isLeaf" placeholder="请输入是否叶子节点:    1:是   0:不是" />
        </el-form-item>
        <el-form-item label="是否缓存该页面:    1:是   0:不是" prop="keepAlive">
          <el-input v-model="form.keepAlive" placeholder="请输入是否缓存该页面:    1:是   0:不是" />
        </el-form-item>
        <el-form-item label="是否隐藏路由: 0否,1是" prop="hidden">
          <el-input v-model="form.hidden" placeholder="请输入是否隐藏路由: 0否,1是" />
        </el-form-item>
        <el-form-item label="是否隐藏tab: 0否,1是" prop="hideTab">
          <el-input v-model="form.hideTab" placeholder="请输入是否隐藏tab: 0否,1是" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="删除状态 0正常 1已删除" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除状态 0正常 1已删除" />
        </el-form-item>
        <el-form-item label="是否添加数据权限1是0否" prop="ruleFlag">
          <el-input v-model="form.ruleFlag" placeholder="请输入是否添加数据权限1是0否" />
        </el-form-item>
        <el-form-item label="外链菜单打开方式 0/内部打开 1/外部打开" prop="internalOrExternal">
          <el-input v-model="form.internalOrExternal" placeholder="请输入外链菜单打开方式 0/内部打开 1/外部打开" />
        </el-form-item>
        <el-form-item label="${comment}" prop="systemId">
          <el-input v-model="form.systemId" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="路径Id" prop="pathId">
          <el-input v-model="form.pathId" placeholder="请输入路径Id" />
        </el-form-item>
        <el-form-item label="全路径" prop="fullPath">
          <el-input v-model="form.fullPath" placeholder="请输入全路径" />
        </el-form-item>
        <el-form-item label="背景图片路径" prop="backImage">
          <image-upload v-model="form.backImage"/>
        </el-form-item>
        <el-form-item label="菜单背景图片" prop="backImage1">
          <el-input v-model="form.backImage1" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPermission, getPermission, delPermission, addPermission, updatePermission } from "@/api/linkage/permission";

export default {
  name: "Permission",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 菜单权限表格数据
      permissionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parentId: null,
        name: null,
        url: null,
        component: null,
        componentName: null,
        redirect: null,
        menuType: null,
        perms: null,
        permsType: null,
        sortNo: null,
        alwaysShow: null,
        icon: null,
        isRoute: null,
        isLeaf: null,
        keepAlive: null,
        hidden: null,
        hideTab: null,
        description: null,
        ruleFlag: null,
        status: null,
        internalOrExternal: null,
        systemId: null,
        pathId: null,
        fullPath: null,
        backImage: null,
        backImage1: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        pathId: [
          { required: true, message: "路径Id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询菜单权限列表 */
    getList() {
      this.loading = true;
      listPermission(this.queryParams).then(response => {
        this.permissionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        parentId: null,
        name: null,
        url: null,
        component: null,
        componentName: null,
        redirect: null,
        menuType: null,
        perms: null,
        permsType: null,
        sortNo: null,
        alwaysShow: null,
        icon: null,
        isRoute: null,
        isLeaf: null,
        keepAlive: null,
        hidden: null,
        hideTab: null,
        description: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
        ruleFlag: null,
        status: null,
        internalOrExternal: null,
        systemId: null,
        pathId: null,
        fullPath: null,
        backImage: null,
        backImage1: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加菜单权限";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPermission(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改菜单权限";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePermission(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPermission(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除菜单权限编号为"' + ids + '"的数据项？').then(function() {
        return delPermission(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('linkage/permission/export', {
        ...this.queryParams
      }, `permission_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
